<?php
class Model_Api_Standard_Guide
{
    private static $_typeid = 106;

    public static function info($id)
    {
        $guide = DB::select()->from('guide')->where('id', '=', $id)->execute()->current();
        $member = Model_Guide::get_info($guide['mid'], 'member');
        $guide['member'] = $member[0];
        $guide['good_language'] = str_replace(',', '，', $guide['good_language']);
        $guide['service_city'] = str_replace(',', '，', $guide['service_city']);
        $guide['member']['litpic'] = Common::img($guide['member']['litpic']);
        $guide['guide_card_pic'] = Common::img($guide['guide_card_pic']);
        $guide['age'] = Model_Guide::get_age($guide['member']['birth_date']);
        $guide['qq'] =   $guide['member']['qq'];
        $guide['wechat'] =   $guide['member']['wechat'];
        $guide['mobile'] =   $guide['member']['mobile'];
        return $guide;
    }

    /**
     * @function 导游服务详情
     * @param $id
     * @return mixed
     */
    public static function get_info($id)
    {
        $service = DB::select()
            ->from('guide_service')
            ->where('id', '=', $id)
            ->and_where('webid', '=', $GLOBALS['sys_webid'])
            ->execute()
            ->current();
        if (! $service)
        {
            return false;
        }

        $piclist = Product::pic_list($service['piclist']);
        $p = array();
        foreach($piclist as &$pic)
        {
            $pi = Model_Api_Standard_System::reorganized_resource_url($pic[0]);
            array_push($p,$pi);
        }
        $service['piclist'] = $p;
        $service['typeid'] = self::$_typeid;
        $service['sellnum'] = Model_Member_Order::get_sell_num($service['id'], 106) + intval($service['bookcount']);
        $service['commentnum'] = Model_Comment::get_comment_num($service['id'], 106); //评论次数
        $service['attrid'] = Model_Guide_Attr::get_attrname_list($service['attrid']);
        $service['jifentprice_info'] = Model_Jifen_Price::get_used_jifentprice($service['jifentprice_id'], self::$_typeid);
        $service['jifenbook_info'] = Model_Jifen::get_used_jifenbook($service['jifenbook_id'], self::$_typeid);
        $service['jifencomment_info'] = Model_Jifen::get_used_jifencomment(self::$_typeid);
        $service['iconlist'] = Product::get_ico_list($service['iconlist']);
        $service['price'] = Currency_Tool::price($service['price']);
        $service['satisfyscore'] = Model_Comment::get_score($service['id'], self::$_typeid, $service['satisfyscore'], $service['commentnum']);//满意度
        //导游信息
        $service['guide'] = self::info($service['guide_id']);
        $service['guide']['guide_card_pic'] = Model_Api_Standard_System::reorganized_resource_url($service['guide']['guide_card_pic']);
        // 获取产品编号
        $service['serial_number'] = St_Product::product_series($id, self::$_typeid);
        // 更加ID查询可用的优惠券
        if(St_Functions::is_normal_app_install('coupon'))
        {
            $service['coupon_list'] = Model_Coupon::get_product_coupon_list($id,self::$_typeid);
        }

        return $service;
    }

    public static function search_result($params, $currentpage, $pagesize = '10')
    {
        $value_arr = array();
        $where = "";
        $sql = 'select * from {table} as s left JOIN (select guide.*,member.truename,member.sex from sline_guide as guide LEFT JOIN sline_member as member on guide.mid=member.mid) as g on s.guide_id=g.id where s.status=6 ';
        //按目的地搜索
        if ($params['destpy'] && $params['destpy'] != 'all')
        {
            $destId = DB::select('id')->from('destinations')->where('pinyin', '=', $params['destpy'])->execute()->get('id');
            $where .= " AND FIND_IN_SET('$destId',s.kindlist) ";
        }
        //价格区间
        if ($params['priceid'])
        {
            $priceArr = DB::select()->from('guide_pricelist')->where('id', '=', $params['priceid'])->execute()->current();
            $where .= " AND s.price BETWEEN {$priceArr['lowerprice']} AND {$priceArr['highprice']} ";
        }
        //性别筛选
        if ($params['sex'] < 2)
        {
            $sex = $params['sex'] == 1 ? '男' : '女';
            $where .= " AND g.sex ='{$sex}'";
        }
        //排序
        if ($params['sorttype'])
        {
            switch ($params['sorttype'])
            {
                case 1:
                    $orderBy = "  s.price ASC,";//价格升序
                    break;
                case 2:
                    $orderBy = "  s.price DESC,";//价格降序
                    break;
                case 3:
                    $orderBy = " s.bookcount DESC,";//销量降序
                    break;
                case 4:
                    $orderBy = " s.recommendnum DESC,";//推荐
                    break;
            }
        }
        //属性
        if ($params['attrid'])
        {
            $where .= str_replace('a.attrid', 's.attrid', Product::get_attr_where($params['attrid']));
        }
        //关键词
        if ($params['keyword'])
        {
            $value_arr[':keyword'] = '%' . $params['keyword'] . '%';
            $where .= " AND s.title like :keyword ";
        }
        //擅长语言
        if ($params['language'])
        {
            $value_arr[':language'] = '%' . $params['language'] . '%';
            $where .= " AND g.good_language like :language ";
        }
        $offset = ($params['p'] - 1) * $pagesize;
        //如果选择了目的地
        if (isset($destId))
        {
            $table = "(select service.*,kind.displayorder from sline_guide_service as service left join sline_kindorderlist as kind on (service.id=kind.aid AND kind.typeid=106 AND service.webid=kind.webid AND kind.classid={$destId}))";
        }
        else
        {
            $table = "(select service.*,kind.displayorder from sline_guide_service as service left join sline_allorderlist as kind on (service.id=kind.aid AND kind.typeid=106 AND service.webid=kind.webid ))";
        }
        $sql = str_replace(array('{table}'), $table, $sql . $where);
        //计算总数
        $total = DB::query(1, $sql)->parameters($value_arr)->execute()->as_array();
        $orderBy = isset($orderBy) ? "{$orderBy}" : '';
        //列表数据
        $arr = DB::query(1, $sql . " order by {$orderBy} IFNULL(s.displayorder,9999) ASC" . " LIMIT {$offset},{$pagesize}")->parameters($value_arr)->execute()->as_array();
        foreach ($arr as &$v)
        {
            $v['commentnum'] = Model_Comment::get_comment_num($v['id'], 106); //评论次数
            $v['sellnum'] = Model_Member_Order::get_sell_num($v['id'], 106) + intval($v['bookcount']); //销售数量
            $v['satisfyscore'] = St_Functions::get_satisfy(self::$_typeid, $v['id'], $v['satisfyscore']);
            $v['attrid'] = Model_Guide_Attr::get_attrname_list($v['attrid']);
            $v['iconlist'] = Product::get_ico_list($v['iconlist']);
            $v['score'] = St_Functions::get_satisfy(self::$_typeid, $v['id'], $v['satisfyscore']);
            $v['price'] = Currency_Tool::price($v['price']);
            $v['litpic'] = Model_Api_Standard_System::reorganized_resource_url($v['litpic']);
        }
        $out = array(
            'total' => count($total),
            'list' => $arr
        );
        return $out;
    }
}